classSolution { public: string maximumOddBinaryNumber(string s){ int n = s.size(), cnt = 0; for (char c : s) { if (c == '1') cnt++; } string ans; for (int i = 0; i < n - 1; i++) { if (i < cnt - 1) ans.push_back('1'); else ans.push_back('0'); } ans.push_back('1'); return ans; } };
static { np[0] = np[1] = true; for (inti=2; i <= N / i; i++) { if (!np[i]) { for (intj= i; j <= N / i; j++) { np[j * i] = true; } } } }
longans=0L;
publiclongcountPaths(int n, int[][] edges) { List<Integer>[] g = newList[n + 1]; Arrays.setAll(g, k -> newArrayList<>()); for (int[] e : edges) { g[e[0]].add(e[1]); g[e[1]].add(e[0]); } dfs(1, 0, g); return ans; }
privateint[] dfs(int x, int fa, List<Integer>[] g) { intzero=0, one = 0; if (np[x]) zero = 1; else one = 1;
for (int y : g[x]) { if (y == fa) continue; int[] t = dfs(y, x, g); ans += (long) zero * t[1] + (long) one * t[0]; if (np[x]) { zero += t[0]; one += t[1]; } else { one += t[0]; } } returnnewint[]{zero, one}; } }